package com.wnm.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnm.pojo.Address;
import com.wnm.pojo.Goods;
import com.wnm.pojo.Order;
import com.wnm.pojo.OrderItem;
import com.wnm.pojo.PageBean;
import com.wnm.pojo.User;
import com.wnm.service.AddressService;
import com.wnm.service.GoodsService;
import com.wnm.service.OrderItemService;
import com.wnm.service.OrderService;
import com.wnm.service.impl.AddressServiceImpl;
import com.wnm.service.impl.GoodsServiceImpl;
import com.wnm.service.impl.OrderItemServiceImpl;
import com.wnm.service.impl.OrderServiceImpl;

@WebServlet("/order/*")
public class OrderServlet extends BaseServlet {
	public void showOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");

		// 展示商品
		BigDecimal totalMoney = new BigDecimal("0");
		String[] goodsid = request.getParameterValues("goodsid");
		String[] nums = request.getParameterValues("nums");
		GoodsService goodsService = new GoodsServiceImpl();
		List<Goods> goodses = new ArrayList<>();
		for (int i = 0; i < goodsid.length; i++) {
			Goods goods = goodsService.goodsDetail(Integer.parseInt(goodsid[i]));
			goodses.add(goods);
			totalMoney = totalMoney.add(goods.getSalesprice().multiply(new BigDecimal(nums[i])));
		}
		AddressService addressService = new AddressServiceImpl();
		List<Address> addresses = addressService.showAddressByUserid(user.getId());
		request.setAttribute("addresses", addresses);
		request.setAttribute("nums", nums);
		request.setAttribute("goodses", goodses);
		request.setAttribute("totalMoney", totalMoney);
		request.getRequestDispatcher("/pages/orderInfor.jsp").forward(request, response);

	}

	public void orderDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String orderid = request.getParameter("orderid");
		OrderService orderService = new OrderServiceImpl();
		Order order = orderService.getById(Integer.parseInt(orderid));
		OrderItemService orderItemService=new OrderItemServiceImpl();
		List<OrderItem> orderItems=orderItemService.findByOrderid(Integer.parseInt(orderid));
		GoodsService goodsService = new GoodsServiceImpl();
		ArrayList<Goods> goodses = new ArrayList<Goods>();
		for (OrderItem orderItem : orderItems) {
			Goods goods=goodsService.goodsDetail(orderItem.getGoodsid());
			goodses.add(goods);
		}
		request.setAttribute("order", order);
		request.setAttribute("goodses", goodses);
		request.setAttribute("orderItems", orderItems);
		request.getRequestDispatcher("/pages/personal_order_details.jsp").forward(request, response);

	}

	public void addOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<OrderItem> orderItems = new ArrayList<>();
		String[] goodsid = request.getParameterValues("goodsid");
		String[] nums = request.getParameterValues("nums");
		for (int i = 0; i < goodsid.length; i++) {
			OrderItem orderItem = new OrderItem();
			Goods goods = new Goods();
			orderItem.setGoodsid(Integer.parseInt(goodsid[i]));
			orderItem.setNums(Integer.parseInt(nums[i]));
			orderItems.add(orderItem);
		}

		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Order order = new Order();
		order.setUserid(user.getId());
		order.setPaytype(request.getParameter("paytype"));
		String address = request.getParameter("addresses");
		String[] list = address.split("-");
		order.setAddress(list[0]);
		order.setTelphone(list[1]);
		order.setAccept(list[2]);
		OrderService orderService = new OrderServiceImpl();
		orderService.add(order, orderItems);
		request.setAttribute("order", order);
		request.getRequestDispatcher("/pages/order_submit.jsp").forward(request, response);

	}

	public void allOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrderService orderService = new OrderServiceImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int num = orderService.getNumsByUserId(user.getId());
		Integer orderNums = num;
		ObjectMapper om = new ObjectMapper();
		om.writeValue(response.getWriter(), orderNums);

	}

	public void nopay(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrderService orderService = new OrderServiceImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int num = orderService.getOrderByStatus(user.getId(), Order.NEW);
		Integer orderNums = num;
		ObjectMapper om = new ObjectMapper();
		om.writeValue(response.getWriter(), orderNums);

	}

	public void noreceiving(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrderService orderService = new OrderServiceImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int num = orderService.getOrderByStatus(user.getId(), Order.SHIPPED);
		Integer orderNums = num;
		ObjectMapper om = new ObjectMapper();
		om.writeValue(response.getWriter(), orderNums);

	}

	public void orderList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrderService orderService = new OrderServiceImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<Order> orders = orderService.getOrdersByuserid(user.getId());
		ObjectMapper om = new ObjectMapper();
		om.writeValue(response.getWriter(), orders);

	}

	public void findPageByuserid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrderService orderService = new OrderServiceImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String currentpage = request.getParameter("currentpage");
		PageBean<Order> orders = orderService.findPageByuserid(user.getId(), Integer.parseInt(currentpage));
		request.setAttribute("pageBean", orders);
		request.getRequestDispatcher("/pages/personal_order.jsp").forward(request, response);

	}
}
